Here is a basic formula for some code that a programmer might have written when designing this circuits simulation.
If (condition1) then (outcome one)
If (condition2) then (outcome two)
Fill in the parenthesis with your predictions of code for opening and closing a switch (and the appropriate outcomes).